﻿// ==UserScript==
// @name ISP_GreyTickets
// @description Set tickets to grey color in techsupport in BILLmanager
// @author yumaa@ispsystem.com
// @include https://my.ispsystem.com/manager/*
// ==/UserScript==

(function() {
if (window.location.hostname != 'my.ispsystem.com') return; // фикс бага оперы про @include

	opera.addEventListener('BeforeEvent.DOMContentLoaded', function(e) {
		if ( ! ujsCheckPage(Array('title','tickets')) ) return;
		var tbl = $id('toolbar-table');
		if ( !tbl ) return;
		var tblrow = tbl.rows[0];
		
		var isnotes = ( tblrow.innerHTML.match(/По клиенту/) ) ? true : false;
		
		tblrow.insertCell(0).innerHTML = '<img src="e.gif" width="12" height="1"/>';
		var onnbtn = tblrow.insertCell(0);
		var offbtn = tblrow.insertCell(0);
		
		// unset grey button
		onnbtn.innerHTML = '<img width="24" height="24" src="t-on.png"/>';
		if ( isnotes ) onnbtn.innerHTML += '<div class="btnlabel">Вкл</div>';
		onnbtn.className = 'toolbtn';
		onnbtn.onmouseover = 'this.className="toolbtn-sel"';
		onnbtn.onmouseout = 'this.className="toolbtn"';
		onnbtn.style = 'cursor: pointer;';
		onnbtn.onclick = function unsetGrey() {
			var i,
				greycookie = {},
				greycookiestr = '';
			var fromcookie = ujsGetCookie('ujsgrey').split(':');
			for (i = 0; i < fromcookie.length; i++)
				greycookie[fromcookie[i]] = 1;
			var maintbl = $id('MAINTBL');
			if ( maintbl )
				for (i = 0; i < maintbl.rows.length; i++) {
					var cell0 = maintbl.rows[i].cells[0];
					if ( cell0.className.match('sel') ) {
						maintbl.rows[i].className = maintbl.rows[i].className.replace(/ ujsgrey/, '');
						//delete greycookie[maintbl.rows[i].cells[0].innerText];
						delete greycookie[ cell0.innerHTML.match(/[0-9]+/)[0] ];
					}
				}
			for (i in greycookie)
				greycookiestr += i + ':';
			document.cookie = 'ujsgrey=' + greycookiestr;
		}
		
		// set grey button
		offbtn.innerHTML = '<img width="24" height="24" src="t-off.png"/>';
		if (isnotes) offbtn.innerHTML += '<div class="btnlabel">Выкл</div>';
		offbtn.className = 'toolbtn';
		offbtn.onmouseover = 'this.className="toolbtn-sel"';
		offbtn.onmouseout = 'this.className="toolbtn"';
		offbtn.style = 'cursor: pointer;';
		offbtn.onclick = function setGrey() {
			var i,
				greycookie = {},
				greycookiestr = '';
			var maintbl = $id('MAINTBL');
			if ( maintbl )
				for (i = 0; i < maintbl.rows.length; i++) {
					var cell0 = maintbl.rows[i].cells[0];
					if ( cell0.className.match('sel') )
						if ( ! maintbl.rows[i].className.match(/ ujsgrey/) ) {
							maintbl.rows[i].className += ' ujsgrey';
							//greycookie[cell0.innerText] = 1;
							greycookie[ cell0.innerHTML.match(/[0-9]+/)[0] ] = 1;
						}
				}
			var fromcookie = ujsGetCookie('ujsgrey').split(':');
			for (i = 0; i < fromcookie.length; i++)
				greycookie[fromcookie[i]] = 1;
			for (i in greycookie)
				greycookiestr += i + ':';
			document.cookie = 'ujsgrey=' + greycookiestr;
		}
		
		// set grey tickets from cookies
		var gcnt = 0, cnt = 0;
		var fromcookie = '';
		var i;
		var greycookie = {};
		var fromcookie = ujsGetCookie('ujsgrey').split(':');
		for (i = 0; i < fromcookie.length; i++)
			greycookie[fromcookie[i]] = 1;
		var maintbl = $id('MAINTBL');
		if ( maintbl )
			for (i = 0; i < maintbl.rows.length /*mistix*/-3; i++) {
				if ( greycookie[maintbl.rows[i].cells[0].innerText] )
					maintbl.rows[i].className += ' ujsgrey';
				if ( maintbl.rows[i].className.match(/\bujsgrey\b/) )
					gcnt++;
			}
		// fix count of tickets, don't count grey ones
		var cntobj = document.getElementById('0-foot');
		if ( cntobj ) {
			var txt = cntobj.innerText.match(/[^\d]+/);
			var cnt = cntobj.innerText.match(/\d+/);
			if ( cnt ) cntobj.innerText = txt + (cnt-gcnt);
		}

	}, true);
})();
